package com.jiepos.mpos.biz.channel.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiepos.mpos.common.constant.DBConstant.Table;
import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.common.dao.InfChannelGrpDao;
import com.jiepos.mpos.common.entity.App_Inf_channel_GrpEntity;
import com.jiepos.mpos.core.entity.Pagination;
import com.jiepos.mpos.core.service.BaseService;
import com.jiepos.mpos.core.util.IDFactoryUtil;
import com.jiepos.mpos.core.util.StringUtils;

/**
 * 
 * 
 * <p>标题: —— 通道组别表服务类</p>
 * <p>模块: 通用平台</p>
 * <p>版权: Copyright (c) 2017 江苏杰博实信息技术有限公司
 * @ProjectName:  [mpos] 
 * @Package com.jiepos.mpos.biz.channel.service 
 * @author 张学涛
 * @version 1.0
 * @since 2017年9月20日上午10:31:23
 * @history（历次修订内容、修订人、修订时间等）
 */
@Service
public class InfChannelGrpService extends BaseService {
	
	@Autowired
	private InfChannelGrpDao infChannelGrpDao;
	
	/**
	 * 查询通道组别列表json分页数据
	 * @param pagination
	 * @param infchannelGrpEntity
	 * @return
	 */
	public Map getChannelGrpList(Pagination pagination,App_Inf_channel_GrpEntity infChannelGrpEntity){
		StringBuffer sb = getInitSql("t1").append(",");
		sb.append("t1.channel_grp_uuid as uuid,t1.channel_grp,t1.descpt from ");
		sb.append(Table.T_APP_INF_CHANNEL_GRP);
		sb.append(" t1 where 1=1 ");
		List list = new ArrayList();
		if (!StringUtils.isBlank(infChannelGrpEntity.getChannel_grp())) {
			sb.append( " and t1.channel_grp like ? " );
			list.add("%" + infChannelGrpEntity.getChannel_grp() + "%");
		}
		pagination.queryString = sb.toString();
		pagination = this.getOrderSql(pagination, "t1");
		return infChannelGrpDao.getPaginationMap(pagination, "t1", list);
	}
	
	/**
	 * 保存通道组别
	 * @param infChannelGrpEntity
	 * @param map
	 */
	public void saveChannelGrp(App_Inf_channel_GrpEntity infChannelGrpEntity,Map map){
		infChannelGrpEntity.setRec_upd_usr_id(String.valueOf(map.get(SystemConstant.SESSION_LOGIN_USER)));
		if (!StringUtils.isBlank(infChannelGrpEntity.getChannel_grp_uuid())) {
			App_Inf_channel_GrpEntity newChannelGrp=infChannelGrpDao.findByParam("channel_grp_uuid", infChannelGrpEntity.getChannel_grp_uuid());
			infChannelGrpEntity.setRec_upd_ts(new Timestamp(System.currentTimeMillis()));
			infChannelGrpEntity.setRec_id(newChannelGrp.getRec_id());
			infChannelGrpEntity.setLast_oper_in(SystemConstant.OPER_UPDATE);
			List noChangeList = new ArrayList();
			noChangeList.add("channel_grp_uuid");
			noChangeList.add("rec_crt_ts");
			infChannelGrpDao.update(infChannelGrpEntity,noChangeList);
		}else{
			infChannelGrpEntity.setRec_crt_ts(new Timestamp(System.currentTimeMillis()));
			infChannelGrpEntity.setRec_upd_ts(new Timestamp(System.currentTimeMillis()));
			infChannelGrpEntity.setChannel_grp_uuid(IDFactoryUtil.getUUID());			
			infChannelGrpEntity.setRec_st(SystemConstant.NORMAL_STATUS);
			infChannelGrpEntity.setLast_oper_in(SystemConstant.OPER_ADD);
			infChannelGrpDao.save(infChannelGrpEntity);
		}
	}
	
	/**
	 * 根据记录id查询实体对象
	 * @param channel_grp_uuid
	 * @return
	 */
	public App_Inf_channel_GrpEntity getChannelGrpById(String channel_grp_uuid){
		if (StringUtils.isBlank(channel_grp_uuid) || channel_grp_uuid.equals("undefined")) {
			return new App_Inf_channel_GrpEntity();
		}
		return infChannelGrpDao.findByParam("channel_grp_uuid", channel_grp_uuid);
	}
	
	
	/**
	 * 根据记录id集合删除数据
	 * @param channel_grp_uuids
	 */
	public void deleteChannelGrpByIds(String channel_grp_uuids){
		infChannelGrpDao.deleteByStatus("channel_grp_uuid", channel_grp_uuids);
	}
	
	
	/**
	 * 校验通道组别
	 * @param infChannelGrpEntity
	 * @return
	 */
	public String checkChannelGrp(App_Inf_channel_GrpEntity infChannelGrpEntity){
		StringBuilder sb = new StringBuilder();
		sb.append("select t1.channel_grp_uuid,t1.channel_grp,t1.descpt from ");		
		sb.append(Table.T_APP_INF_CHANNEL_GRP);
		sb.append(" t1 where 1=1 ");
		List list = new ArrayList();
		if (!StringUtils.isBlank(infChannelGrpEntity.getChannel_grp())) {
			sb.append( " and t1.channel_grp = ? " );
			list.add(infChannelGrpEntity.getChannel_grp());
		}			
		if (!StringUtils.isBlank(infChannelGrpEntity.getChannel_grp_uuid())) {
			sb.append( " and t1.channel_grp_uuid != ? " );
			list.add(infChannelGrpEntity.getChannel_grp_uuid());
		}		
		List result=infChannelGrpDao.queryForList(sb.toString(), list);
		if(result==null||result.size()==0){
			return SystemConstant.SUCCESS;
		}
		return SystemConstant.ERROR;
	}

}
